#include "CGame.h"

#pragma region Cons & Dest


CGame::CGame(void)
{
}


CGame::~CGame(void)
{
}

#pragma endregion

#pragma region Methods

void CGame::Game_Init(HINSTANCE hInst)
{
	m_wf = new CWinForm(hInst);
	m_dxDev = new CDxDevice(m_wf->GetHWND());
	m_wf->Show();
	m_timer = CTimer::GetInstance();
	// bat dau vong timer
	m_timer->Start();
	//m_gameTime = CGameTime::GetInstance();
	//timer = clsTimer::getInstance();
	m_keyboard = new CInput(m_wf->GetHWND());
	D3DXCreateSprite(m_dxDev->GetDevice(),&m_spriteDX);
	/*D3DXCreateSprite(dxDev->getDevice(),&sprite_handler);*/
	m_texture = new CTexture();
	m_texture->LoadTexture(m_dxDev->GetDevice(),m_spriteDX,"background.bmp",D3DCOLOR_ARGB(255,255,0,255));
	m_soundPlayer = new CSoundPlayer();
	if(FAILED(m_soundPlayer->Initialise(m_wf->GetHWND())))
	{
		return;
	}

	// add sound files
	if(!m_soundPlayer->AddWav("drumroll.wav",&id))
	{
		return;
	}
	m_soundPlayer->PlaySound(0,true);

}

void CGame::Game_Run()
{
	if(m_dxDev->GetDevice()==NULL)
	{
		return;
	}
	m_dxDev->GetDevice()->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0);
	HRESULT result = m_keyboard->GetKeyboardState();
	if(result!=DI_OK)
	{
		MessageBox(NULL,"Keyboard cannot be accessed","Error",MB_OK | MB_ICONERROR);
		return;
	}
	m_timer->Update();
	if(m_dxDev->GetDevice()->BeginScene())
	{
		m_spriteDX->Begin( D3DXSPRITE_ALPHABLEND);
		m_dxDev->GetDevice()->Clear(1,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),0,0);

		/*RECT r = {0, 4, 223, 53 + 4};
		texture->Draw(0,0,1.0f,1.0f,D3DXVECTOR2(0,0),0,D3DCOLOR_ARGB(255,255,255,255),&r,1.0f);
		r.left = 0;
		r.top = 56;
		r.right = 210;
		r.bottom = 57 + 56;
		texture->Draw(0,60,1.0f,1.0f,D3DXVECTOR2(0,0),0,D3DCOLOR_ARGB(255,255,255,255),&r,1.0f );
		r.left = 0;
		r.top = 110;
		r.right = 174;
		r.bottom = 61 + 110;
		texture->Draw(0,120,1.0f,1.0f,D3DXVECTOR2(0,0),0,D3DCOLOR_ARGB(255,255,255,255),&r,1.0f );*/
	
// 		m_dxDev->LoadImageFromFile("background.bmp",D3DCOLOR_ARGB(255,255,255,255));
// 		m_dxDev->GetDevice()->StretchRect(m_dxDev->GetSurface(),
// 								NULL,
// 								m_dxDev->GetBackbuffer(),
// 								NULL,
// 								D3DTEXF_NONE);
		RECT r= {200,100,500,500};
		m_texture->Draw(200,100,1.0f,1.0f,D3DXVECTOR2(0,0),0,D3DCOLOR_ARGB(255,255,255,255),&r,0.0f);

		//m_texture->Draw(10,10);

		m_spriteDX->End();
		m_dxDev->GetDevice()->EndScene();
	}
	m_dxDev->GetDevice()->Present(NULL,NULL,NULL,NULL);
}

void CGame::Game_End()
{
	m_timer->Stop();
	if(m_dxDev->GetDevice()!=NULL)
	{
		m_dxDev->GetDevice()->Release();
	}
	if(m_dxDev->GetD3D()!=NULL)
	{
		m_dxDev->GetD3D()->Release();
	}
	m_keyboard->KillKeyboard();
}

void CGame::FrameAdvanced()
{
	m_gameTime->Update(60);
	// get current FPS
	m_currentFrameRate = (unsigned short)m_gameTime->GetFPS();
	// set window text
	char c_FPS[20];
	if(m_currentFrameRate != m_lastFrameRate)
	{
		_itot(m_currentFrameRate,c_FPS,10);
		m_lastFrameRate = m_currentFrameRate;
		SetWindowText(m_wf->GetHWND(),c_FPS);
	}
}

#pragma endregion 